package com.htj.kf2b.construct_mobile.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.StrSplitter;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.htj.cross.util.Des;
import com.htj.cross.webservice.CrossService;
import com.htj.cross.webservice.CrossService_Service;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.tempuri.ArrayOfString;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/timeproxy")
public class TimeProxyController {


    /**
     * 获取新的委托数据,查询state为0的委托
     *
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/newproxy", method = RequestMethod.GET)
    public JSONObject getProxyByPhone(String phone) {

        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getundonumber", "A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        System.out.println(r);


        //TODO 获取code 通过code获取
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("messageNumber", r);

        return jsonObject;
    }



     /**
     * 删除委托
     *
     * @param
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/deleteproxy", method = RequestMethod.GET)
    public JSONObject deleteProxy(String id) {
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.deleteTimeProxy", "A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();

        data.getString().add(id);/*内外方法的参数*/
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);

        JSONObject jsonObject = JSONUtil.createObj();

        if(r.equals("1")){
            jsonObject.putOnce("code", "200");
            jsonObject.putOnce("msg", "成功");
        }else {
            jsonObject.putOnce("code", "404");
            jsonObject.putOnce("msg", "失败");
        }


        return jsonObject;
    }


    /**
     * 修改委托状态
     *
     * @param
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/updateproxy", method = RequestMethod.GET)
    public JSONObject updateProxy(String id,String status) {
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.updateTimeProxy", "A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();

        data.getString().add(id);/*内外方法的参数*/
        data.getString().add(status);
        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
        System.out.println(r);

        JSONObject jsonObject = JSONUtil.createObj();

        if(r.equals("1")){
            jsonObject.putOnce("code", "200");
            jsonObject.putOnce("msg", "成功");
        }else {
            jsonObject.putOnce("code", "404");
            jsonObject.putOnce("msg", "失败");
        }
        return jsonObject;
    }


    /**
     * 查询 我创建的委托
     * 如果 message = undefined 则显示暂无委托
     *
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/proxycreated", method = RequestMethod.GET)
    public JSONObject proxyCreated(String phone, String startMark) {
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getTimeEntrustList","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString().add(startMark);
        data.getString ().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);

        String resultBase64 = Base64.decodeStr(r);
        String resultSting = resultBase64.substring(0, resultBase64.length() - 1);

        System.out.println(resultSting);

        ArrayList arrayList = new ArrayList();


        List<String> split = StrSplitter.split(resultSting, '}', 0, true, true);
        System.out.println(split);


        for (String s : split) {
            System.out.println(s.substring(1)+"}");
            arrayList.add(JSONUtil.parseObj(s.substring(1)+"}"));
        }


        //TODO 获取code 通过code获取
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("message", arrayList);
        jsonObject.putOnce("phone", phone);
        jsonObject.putOnce("startMark", startMark);
        return jsonObject;
    }


     /**
     * 查询 我收到的委托
     * 如果 message = undefined 则显示暂无委托
     *
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/proxyaccepted", method = RequestMethod.GET)
    public JSONObject proxyAccepted(String phone, String startMark) {

        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.getTimeEntrustedList","A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();
        data.getString().add(phone);/*内外方法的参数*/
        data.getString().add(startMark);
        data.getString ().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);
         String resultBase64 = Base64.decodeStr(r);
        String resultSting = resultBase64.substring(0, resultBase64.length() - 1);

        System.out.println(resultSting);

        ArrayList arrayList = new ArrayList();


        List<String> split = StrSplitter.split(resultSting, '}', 0, true, true);
        System.out.println(split);


        for (String s : split) {
            System.out.println(s.substring(1)+"}");
            arrayList.add(JSONUtil.parseObj(s.substring(1)+"}"));
        }



        //TODO 获取code 通过code获取
        JSONObject jsonObject = JSONUtil.createObj();
        jsonObject.putOnce("code", "200");
        jsonObject.putOnce("msg", "成功");
        jsonObject.putOnce("message", arrayList);
        jsonObject.putOnce("phone", phone);
        jsonObject.putOnce("startMark", startMark);
        return jsonObject;
    }



    /**
     * 根据
     *
     * @param
     * @return
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/setproxy", method = RequestMethod.GET)
    public JSONObject setProxy(String phone, String entrustedId,String entrustedName, String reason, String startTime,String endTime) {
        CrossService_Service cs = new CrossService_Service();
        CrossService service = cs.getCrossServiceImplPort();
        Des ds = new Des();/*加密文件*/
        String str = ds.strEnc("construct.CallSpring.inserTimeProxy", "A08FAEF81E115E2D075", "", "");/*第一个参数是程序集.文件名.方法名 第二个参数是内网配置的秘钥*/
        ArrayOfString data = new ArrayOfString();



         data.getString().add(phone);/*内外方法的参数*/
         data.getString().add(entrustedId);/*内外方法的参数*/
         data.getString().add(Base64.encode(entrustedName).replace(" ","").replace("+",""));
         data.getString().add(Base64.encode(reason).replace(" ","").replace("+",""));//*内外方法的参数*/*内外方法的参数*/
         data.getString().add(startTime);/*内外方法的参数*/
         data.getString().add(endTime);/*内外方法的参数*/


        data.getString().add("");/*预留参数*/
        data.getString().add("");/*预留参数*/
        String r = service.jGetJson(str, "construct", data);


        JSONObject jsonObject = JSONUtil.createObj();

        if(r.equals("success")){
            jsonObject.putOnce("code", "200");
            jsonObject.putOnce("msg", "成功");
        }else {
            jsonObject.putOnce("code", "404");
            jsonObject.putOnce("msg", "失败");
        }


        return jsonObject;
    }



}
